package cn.superchart.common.base;
import cn.superchart.common.enums.ExceptionEnum;
import cn.superchart.common.utils.Checker;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaseResult<T> implements Serializable{
    private static final long serialVersionUID = 1L;
    public static final int CODE_SUCCESS = 200;
    public static final int CODE_FAIL = 500;

    private int code;
    private String msg;
    private T data;

    private BaseResult(int code, String msg, T data) {
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    /**
     * 获取code
     * @return code
     */
    public Integer getCode() {
        return this.code;
    }

    /**
     * 获取msg
     * @return msg
     */
    public String getMsg() {
        return this.msg;
    }
    /**
     * 获取data
     * @return data
     */
    public T getData() {
        return this.data;
    }

    /**
     * 给code赋值，连缀风格
     * @param code code
     * @return 对象自身
     */
    public BaseResult<T> setCode(int code) {
        this.code = code;
        return this;
    }

    /**
     * 给msg赋值，连缀风格
     * @param msg msg
     * @return 对象自身
     */
    public BaseResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    /**
     * 给data赋值，连缀风格
     * @param data data
     * @return 对象自身
     */
    public BaseResult<T> setData(T data) {
        this.data = data;
        return this;
    }


    // 构建成功
    public static <T> BaseResult<T> ok() {
        return new BaseResult<>(CODE_SUCCESS, "操作成功", null);
    }
    public static <T> BaseResult<T> ok(String msg) {
        return new BaseResult<>(CODE_SUCCESS, msg, null);
    }
    public static <T> BaseResult<T> code(int code) {
        return new BaseResult<>(code, null, null);
    }
    public static <T> BaseResult<T> result(T data) {
        return new BaseResult<>(CODE_SUCCESS, "操作成功", data);
    }

    // 构建失败
    public static <T> BaseResult<T> error() {
        return new BaseResult<>(CODE_FAIL, "服务器异常", null);
    }
    public static <T> BaseResult<T> error(String msg) {
        return new BaseResult<>(CODE_FAIL, msg, null);
    }


    public static <T> BaseResult<T> error(String msg,Object ... params) {
        if(Checker.BeNotEmpty(params) && Checker.BeNotBlank(msg)){
            Pattern pattern = Pattern.compile("\\{\\}");
            Matcher matcher = pattern.matcher(msg);
            int i = 0;
            while (matcher.find() && i < params.length) {
                msg = msg.replaceFirst("\\{}", String.valueOf(params[i++]));
            }
        }
        return new BaseResult<>(CODE_FAIL, msg, null);
    }


    // 构建指定状态码
    public static <T> BaseResult<T> by(int code, String msg, T data) {
        return new BaseResult<>(code, msg, data);
    }

    public static <T> BaseResult<T> by(ExceptionEnum exceptionEnum, T data) {
        return new BaseResult<>(exceptionEnum.getCode(), exceptionEnum.getMsg(), data);
    }


    /*
     * toString()
     */
    @Override
    public String toString() {
        return "{"
                + "\"code\": " + this.getCode()
                + ", \"msg\": \"" + this.getMsg() + "\""
                + ", \"data\": \"" + this.getData() + "\""
                + "}";
    }
}
